package mta.app;

import org.apache.commons.cli.*;

/**
 * Magento Translation Assistant
 * Copyright (c) 2009 Dmitry Nikolaev
 * Contact author: blackliteon@gmail.com
 */
public class ApplicationBuilderOptions {

    private String[] args;
    private Options options = new Options();

    public Options getOptions() {
        return options;
    }

    public ApplicationBuilderOptions(String[] args) {
        this.args = args;

        options.addOption("m", "merge", false, "Merge two localization packages");
        options.addOption("h", "help", false, "Print help message");
        options.addOption("g", "generate", false, "Generate patch");
        options.addOption("p", "patch", false, "Apply patch");
    }

    public enum Command {
        Help("h"),
        MergeLocalizations("m"),
        GeneratePatch("g"),
        Patch("p");

        private String value;

        Command(String value) {
            this.value = value;
        }

        public String stringValue() {
            return value;
        }
    }

    private Command getCommand(CommandLine cl) throws ParseException {
        Command result = null;
        int selected = 0;

        for (Command command : Command.values()) {
            if (cl.hasOption(command.stringValue())) {
                result = command;
                selected++;
            }
        }

        if (selected == 0) {
            throw new ParseException("Please, specify operation. For more information try '-help'");
        }

        if (selected > 1) {
            throw new ParseException("Please specify only one operation");
        }

        return result;
    }

    public Command getCommand() throws ParseException {
        CommandLineParser parser = new PosixParser();
        CommandLine cl = parser.parse(options, args);

        return getCommand(cl);
    }

}
